package logic

import (
	"context"
	"edu-pro/models/mysql"

	"edu-pro/edurpc/edurpc"
	"edu-pro/edurpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectListLogic {
	return &CollectListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CollectListLogic) CollectList(in *edurpc.CollectListRequest) (*edurpc.CollectListResponse, error) {
	//收藏列表
	var lists []mysql.CollectData
	mysql.DB.Debug().Model(&mysql.Collection{}).
		Select("users.nickname,courses.title,courses.learn_count").
		Joins("left join users on users.id = collections.user_id").
		Joins("left join courses on courses.id = collections.course_id").
		Scopes(mysql.Paginate(int(in.Page), int(in.Size))).
		Find(&lists)
	var listData []*edurpc.CollectList
	for _, data := range lists {
		list := edurpc.CollectList{
			Title:      data.Title,
			Nickname:   data.Nickname,
			LearnCount: data.LearnCount,
		}
		listData = append(listData, &list)
	}

	return &edurpc.CollectListResponse{
		List: listData,
	}, nil
}
